<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.min.js"></script>
    <style>
        .content {
            height: 300px;
            overflow: scroll;
            border: 1px solid #999
        }

        .action {
            margin-top: 10px;
        }

        .flex-end {
            display: flex;
            justify-content: flex-end;
        }

        .well {
            display: inline-block;
            background-color: #98e855;
            padding: 10px;
            margin: 10px 0;
            border-radius: 10px;
        }
    </style>
</head>
<!-- socket.io -->
<body>
    <div id="app">
        <h1>{{title}}</h1>
        <div ref="content" class="content">
            <div v-for="item in msgList" :key="item.id">
                <div :class="{'flex-end':item.type===2}">
                    <div class="well">
                        {{item.from}}:
                        <span v-html="item.content"></span>
                    </div>
                </div>
            </div>
            <div ref="end"></div>
        </div>
        <div class="action">
            <form @submit.prevent="submit">
                <input v-model.trim="msg" type="text" placeholder="请输入内容">
                <button>发送</button>
                <button type="button" @click="msgList=[]">清空消息</button>
            </form>
        </div>
    </div>
    <script>
        // 创建vue应用实例
        const app = new Vue({
            el: "#app",
            data() {
                return {
                    title: '智能客服',
                    msg: '',
                    // 记录消息内容的数组
                    msgList: []
                }
            },
            created() {
                // 创建ws实例
                this.ws = new WebSocket('ws://localhost:8888')
                // 监听服务端的消息
                this.ws.onmessage = (e) => {
                    console.log(e.data, 'onmessage');
                    const content = e.data.replaceAll('{br}', '<br/>')
                    app.msgList.push({
                        id: Date.now(),
                        content,
                        from: '服务器',
                        type: 1          // 服务器发送的消息
                    })
                    // this.$refs.content.scrollTop=10000
                    setTimeout(() => {
                        // 让目标元素出现在可视区域
                        this.$refs.end.scrollIntoView({ behavior: 'smooth' })
                        // this.$refs.content.scrollTop = this.$refs.content.scrollHeight
                    }, 0)
                }
                // 监听通信通道是否建立
                this.ws.onopen = function (e) {
                    // console.log(e, 'onopen');
                    // 发送消息
                    // ws.send('你好服务器')
                }
                // 监听通信通道的关闭
                this.ws.onclose = function (e) {
                    console.log(e, 'onclose');
                }
            },
            methods: {
                submit() {
                    if (this.msg === '') {
                        return alert('请输入消息内容')
                    }
                    // 发送消息给服务端
                    this.ws.send(this.msg)
                    // 客户端发送的消息
                    this.msgList.push({
                        id: Date.now(),
                        content: this.msg,
                        from: '客户端',
                        type: 2      // 客户端消息 
                    })
                    this.msg = ''
                    setTimeout(() => {
                        // 让目标元素出现在可视区域
                        this.$refs.end.scrollIntoView({ behavior: 'smooth' })
                        // this.$refs.content.scrollTop = this.$refs.content.scrollHeight
                    }, 0)
                }
            }
        })

    </script>
</body>

</html>